package controllers

import (
	"encoding/json"
	_ "fmt"
	_ "gitchina/kly_service/errors"
	"gitchina/kly_service/models"
	"gitchina/kly_service/util"
	"github.com/astaxie/beego"
	_ "io/ioutil"
)

type LogisticsController struct {
	beego.Controller
}

// @router /102001 [get]
func (l *LogisticsController) GetAreaList() {
	var logi models.Logistics
	l.Data["json"] = models.NewOkResponse(logi.GetAreaMap(l.GetString("id", "")))
	l.ServeJSON()
}

// @router /102003 [post]
func (l *LogisticsController) PubCarrier() {
	var rq models.CarrierPubRQ
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq)
	if err == nil {
		pubId, err := logi.CarrierPub(mbrId, &rq)
		if err != nil {
			l.Data["json"] = models.NewFailResponse(err)
		} else {
			l.Data["json"] = models.NewOkResponse(pubId)
		}
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102004 [post]
func (l *LogisticsController) GetCarrierPub() {
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	pubRS, err := logi.GetCarrierPub(mbrId)
	if err != nil {
		l.Data["json"] = models.NewFailResponse(err)
	} else {
		r := models.NewOkResponse(pubRS)
		if pubRS.Id == "" {
			r.Result = nil
		}
		l.Data["json"] = r
	}
	l.ServeJSON()
}

// @router /102005 [post]
func (l *LogisticsController) PubLine() {
	var rq models.LinePubRQ
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq)
	if err == nil {
		pubId, err := logi.LinePub(mbrId, &rq)
		if err != nil {
			l.Data["json"] = models.NewFailResponse(err)
		} else {
			l.Data["json"] = models.NewOkResponse(pubId)
		}
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}

}

// @router /102006 [post]
func (l *LogisticsController) UpdateLine() {
	var rq models.LineUpdateRQ
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq)
	if err == nil {
		err := logi.UpdateLine(mbrId, &rq)
		if err != nil {
			l.Data["json"] = models.NewFailResponse(err)
		} else {
			l.Data["json"] = models.NewOkResponse(nil)
		}
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}

}

// @router /102007 [post]
func (l *LogisticsController) GetLinePub() {
	var logi models.Logistics
	var rq models.GetLinePubRQ
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
		rs, err := logi.GetLinePubs(mbrId, rq)
		if err != nil {
			l.Data["json"] = models.NewFailResponse(err)
		} else {
			l.Data["json"] = models.NewOkResponse(rs)
		}
		util.GetLogger().Debug("[ctrl-GetLinePub] - debug:%v", rs)
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102008 [post]
func (l *LogisticsController) GetCarrierPubList() {
	var logi models.Logistics
	var rq models.QueryCarrierPubListRQ
	err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq)
	if err == nil {
		rs, err := logi.QueryCarrierPubList(rq)
		if err != nil {
			l.Data["json"] = models.NewFailResponse(err)
		} else {
			l.Data["json"] = models.NewOkResponse(rs)
		}
		util.GetLogger().Debug("[ctrl-GetCarrierPubList] - debug:%v", rs)
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102009 [get]
func (l *LogisticsController) GetCarrierPubDetail() {
	var logi models.Logistics
	rs, err := logi.GetCarrierPubDetail(models.ID(l.GetString("carrier_pub_id", "1")))
	if err != nil {
		l.Data["json"] = models.NewFailResponse(err)
	} else {
		l.Data["json"] = models.NewOkResponse(rs)
	}
	l.ServeJSON()
}

// @router /102010 [post]
func (l *LogisticsController) GetLinePubList() {
	var logi models.Logistics
	var rq models.QueryLinePubListRQ
	err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq)
	if err == nil {
		rs, err := logi.QueryLinePubList(rq)
		if err != nil {
			l.Data["json"] = models.NewFailResponse(err)
		} else {
			l.Data["json"] = models.NewOkResponse(rs)
		}
		util.GetLogger().Debug("[ctrl-GetLinePubList] - debug:%v", rs)
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102011 [get]
func (l *LogisticsController) GetLinePubDetail() {
	var logi models.Logistics
	rs, err := logi.GetLinePubDetail(models.ID(l.GetString("line_pub_id", "1")))
	if err != nil {
		l.Data["json"] = models.NewFailResponse(err)
	} else {
		l.Data["json"] = models.NewOkResponse(rs)
	}
	l.ServeJSON()
}

// @router /102012 [post]
func (l *LogisticsController) AddOrUpdateLogsticsCorp() {
	var rq models.AddOrUpdateLogisticsCorpRQ
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq)
	if err == nil {
		_, err := logi.AddOrUpdateLogisticsCorp(mbrId, rq)
		if err != nil {
			l.Data["json"] = models.NewFailResponse(err)
		} else {
			l.Data["json"] = models.NewOkResponse(nil)
		}
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102013 [post]
func (l *LogisticsController) GetLogsticsCorp() {
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	rs := logi.GetLogisticsCorp(mbrId)
	l.Data["json"] = models.NewOkResponse(rs)
	l.ServeJSON()
}

// @router /102014 [get]
func (l *LogisticsController) GetInvoiceCorps() {
	var logi models.Logistics
	rs := logi.GetInvoiceCorps()
	l.Data["json"] = models.NewOkResponse(rs)
	l.ServeJSON()
}

// @router /102015 [post]
func (l *LogisticsController) AddInvoicePostAddr() {
	var rq models.InvoicePostAddr
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		if id, err := logi.AddInvoicePostAddr(mbrId, &rq); err != nil {
			l.Data["json"] = models.NewFailResponse(err)
		} else {
			l.Data["json"] = models.NewOkResponse(id)
		}
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102016 [post]
func (l *LogisticsController) UpdateInvoicePostAddr() {
	var rq models.InvoicePostAddr
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		if err = logi.UpdateInvoicePostAddr(mbrId, &rq); err != nil {
			l.Data["json"] = models.NewFailResponse(err)
		} else {
			l.Data["json"] = models.NewOkResponse(nil)
		}
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102017 [post]
func (l *LogisticsController) DeleteInvoicePostAddr() {
	var rq models.InvoicePostAddr
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		if err = logi.DeleteInvoicePostAddr(mbrId, rq.Id); err != nil {
			l.Data["json"] = models.NewFailResponse(err)
		} else {
			l.Data["json"] = models.NewOkResponse(nil)
		}
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102018 [post]
func (l *LogisticsController) GetInvoicePostAddrs() {
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	rs := logi.GetInvoicePostAddrs(mbrId)
	l.Data["json"] = models.NewOkResponse(rs)
	l.ServeJSON()
}

// @router /102019 [post]
func (l *LogisticsController) GetLastInvoicePostAddr() {
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	rs := logi.GetLastInvoicePostAddr(mbrId)
	l.Data["json"] = models.NewOkResponse(rs)
	l.ServeJSON()
}

// @router /102020 [post]
func (l *LogisticsController) CancelCarrierPub() {
	var rq models.GetCarrierPubRQ
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		logi.CancelCarrierPub(mbrId, rq.Id)
		l.Data["json"] = models.NewOkResponse(nil)
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102021 [post]
func (l *LogisticsController) CancelLine() {
	var rq models.LineUpdateRQ
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		logi.CancelLine(mbrId, rq.Id)
		l.Data["json"] = models.NewOkResponse(nil)
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102022 [post]
func (l *LogisticsController) UpdateLogisticsBiz() {
	var rq models.LogisticsBiz
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		logi.UpdateLogisticsBiz(mbrId, &rq)
		l.Data["json"] = models.NewOkResponse(nil)
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102023 [post]
func (l *LogisticsController) GetLogisticsBiz() {
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	rs := logi.GetLogisticsBiz(mbrId)
	l.Data["json"] = models.NewOkResponse(rs)
	l.ServeJSON()
}

// @router /102024 [post]
func (l *LogisticsController) GetRecommendLineList() {
	var rq models.GetRecommendLineListRQ
	var logi models.Logistics
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		rs := logi.GetRecommendLineList(&rq)
		l.Data["json"] = models.NewOkResponse(rs)
		util.GetLogger().Debug("[ctrl-GetRecommendLineList] - debug:%d - %v", rq.StartArea, rs)
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102025 [post]
func (l *LogisticsController) GetLogisticsCorpDetail() {
	var rq models.GetLogisticsCorpDetailRQ
	var logi models.Logistics
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		rs := logi.GetLogisticsCorpDetail(rq.Id)
		l.Data["json"] = models.NewOkResponse(rs)
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102026 [post]
func (l *LogisticsController) RecoverLine() {
	var rq models.LineUpdateRQ
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		logi.RecoverLine(mbrId, rq.Id)
		l.Data["json"] = models.NewOkResponse(nil)
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102027 [post]
func (l *LogisticsController) GetLinePubWithoutLogin() {
	var logi models.Logistics
	var rq models.GetLinePubRQ
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		rs, err := logi.GetLinePubs(rq.Id, rq)
		if err != nil {
			l.Data["json"] = models.NewFailResponse(err)
		} else {
			l.Data["json"] = models.NewOkResponse(rs)
		}
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102028 [post]
func (l *LogisticsController) GetLogistcsCorpList() {
	var logi models.Logistics
	var rq models.GetLogisticsCorpListRQ
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		rs := logi.GetLogisticsCorpList(&rq)
		l.Data["json"] = models.NewOkResponse(rs)
		util.GetLogger().Debug("[ctrl-GetLogistcsCorpList] - debug:%v", rs)
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102029 [post]
func (l *LogisticsController) CanUseInvoice() {
	var logi models.Logistics
	mbrId := models.ID(l.Ctx.Input.GetData("mbrId").(string))
	if ok, err := logi.CanUseInvoice(mbrId); err == nil {
		l.Data["json"] = models.NewOkResponse(ok)
	} else {
		l.Data["json"] = models.NewFailResponse(err)
	}
	l.ServeJSON()
}

// @router /102030 [post]
func (l *LogisticsController) AddTransportLog() {
	var rq models.TransportLog
	var logi models.Logistics
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		if state, err := logi.AddTransportLog(&rq); err != nil {
			l.Data["json"] = models.NewFailResponse(err)
		} else {
			l.Data["json"] = models.NewOkResponse(state)
		}
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102031 [post]
func (l *LogisticsController) GetTransportLogs() {
	var rq models.TransportLog
	var logi models.Logistics
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		rs := logi.GetTransportLogs(rq.WaybillId, rq.Offset)
		l.Data["json"] = models.NewOkResponse(rs)
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /102032 [post]
func (l *LogisticsController) GetLastTransportLog() {
	var rq models.TransportLog
	var logi models.Logistics
	if err := json.Unmarshal(l.Ctx.Input.RequestBody, &rq); err == nil {
		rs := logi.GetLastTransportLog(rq.WaybillId)
		l.Data["json"] = models.NewOkResponse(rs)
		l.ServeJSON()
	} else {
		l.Ctx.ResponseWriter.WriteHeader(400)
	}
}
